package mo.offer_version2;

import java.util.HashMap;
import java.util.Map;

public class L48 {

    public int lengthOfLongestSubstring(String s) {
        if (s.length() == 0) {
            return 0;
        }
        Map<Character, Integer> char2count = new HashMap<>();
        char2count.put(s.charAt(0), 0);
        int start = 0;
        int max = 1;
        int count = 1;
        for (int i = 1; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!char2count.containsKey(c)) {
                count++;
            } else {
                int last = char2count.get(c);
                if (last >= start) {
                    start = last + 1;
                    count = i - last;
                } else {
                    count++;
                }
            }
            char2count.put(c, i);
            max = Math.max(max, count);
        }
        return max;
    }

    public static void main(String[] args) {
        L48 test = new L48();
        System.out.println(test.lengthOfLongestSubstring("tmmzuxt"));
    }

}
